#include "business.h"
#define LOG_TAG          "BUSI_MODULE"
#include "elog.h"

// business 任务句柄
TaskHandle_t            business_handler;

busi_sample_share_value_t busi_sample_share_data;
mmc_ctrl_t busi_sample_mmc;

busi_control_share_value_t busi_control_share_data;
mmc_ctrl_t busi_control_mmc;



void busi_process_task_entry(void* p);
/**
 * @desc  : business层处理任务初始化
 * @date  : 2025-11-04
 * @author: AnRui
 * @note  :
 */
void busi_layer_init(void)
{
    log_d("busi_process_task init start");
    // 初始化busi和采集模块交互结构体
    mmc_ctrl_init(&busi_sample_mmc, (void*)&busi_sample_share_data, sizeof(busi_sample_share_value_t));
    busi_control_share_data.fan_status = FAN_STATUS_OFF;
    busi_control_share_data.light_status = LIGHT_STATUS_OFF;
    // 初始化busi和控制模块交互结构体
    mmc_ctrl_init(&busi_control_mmc, (void*)&busi_control_share_data, sizeof(busi_control_share_value_t));

    // 创建任务
    if (xTaskCreate(busi_process_task_entry,
        "business_task",
        BUSI_TASK_STACK_SIZE,
        NULL,
        BUSI_TASK_PRIORITY,
        &business_handler) != pdTRUE) {
        log_e("business_moduel_task create failer");
    }
    log_d("busi_process_task init end");
}

/**
 * @desc  : business层处理任务入口函数
 * @date  : 2025-11-04
 * @author: AnRui
 * @note  :
 */
void busi_process_task_entry(void* p)
{
    log_d("busi_process_task start");
    TickType_t PreviousWakeTime = xTaskGetTickCount();
    bool result;
    float real_temp;
    while (1) {
        // 向采集模块发送采集请求
        result = mmc_ctrl_require(&busi_sample_mmc, BUSI_WAIT_SAMPLE_TIME);
        if (!result) {
            log_e("busi sample communite failer");
            // 错误处理：直接延时，无需goto
            vTaskDelayUntil(&PreviousWakeTime, BUSI_TASK_INTERVAL_TIME);
            continue;
        }

        // 判断温度数据是否有效
        if (!busi_sample_share_data.temp_valid) {
            log_e("temperature device failure");
        } else {
					  real_temp = busi_sample_share_data.temp / 1000.0f;
            log_i("sample moduel temperature %02f",real_temp);
            // 根据采集数据 更新控制数据
            if (real_temp >= BUSI_SAMPLE_TEMPRATURE_HIGH_THRESHOLD) {
                busi_control_share_data.fan_status = FAN_STATUS_ON;
                log_d("temperature reach high threshold (%.3f°C), fan turn on", real_temp);
            } else if (real_temp <= BUSI_SAMPLE_TEMPRATURE_LOW_THRESHOLD) {
                busi_control_share_data.fan_status = FAN_STATUS_OFF;
                log_d("temperature reach low threshold (%.3f°C), fan turn off", real_temp);
            } else {
                log_d("temperature in middle threshold (%.3f°C), fan keep current status", real_temp);
            }
        }

        // 判断亮度数据是否有效
        if (!busi_sample_share_data.bright_valid) {
            log_e("bright device failure");
        } else {
            log_i("sample moduel brightness %d", busi_sample_share_data.brightness);
            // 阈值控制：高亮度关、低亮度开、中间区间保持当前状态
            if (busi_sample_share_data.brightness >= BUSI_SAMPLE_BRLIGHTNESS_HIGH_THRESHOLD) {
                busi_control_share_data.light_status = LIGHT_STATUS_OFF;
                log_d("brightness reach high threshold, light turn off");
            } else if (busi_sample_share_data.brightness <= BUSI_SAMPLE_BRLIGHTNESS_LOW_THRESHOLD) {
                busi_control_share_data.light_status = LIGHT_STATUS_ON;
                log_d("brightness reach low threshold, light turn on");
            } else {
                log_d("brightness in middle threshold, light keep current status");
            }
        }

				log_i("fan_status : %s , light_status : %s",
                busi_control_share_data.fan_status == FAN_STATUS_ON ? "风扇打开" : "风扇关闭",
                busi_control_share_data.light_status == LIGHT_STATUS_ON ? "电灯打开" : "电灯关闭");
				
        // 向控制模块发送控制请求
        result = mmc_ctrl_require(&busi_control_mmc, BUSI_WAIT_COTR_TIME);
        if (!result) {
            log_e("busi_control_communite_failer");
        }
				
				if(LIGHT_STATUS_FAULT==busi_control_share_data.fan_status){
					log_e("light device is fault");
				}
				if(FAN_STATUS_FAULT==busi_control_share_data.fan_status){
					log_e("fan device is fault");
				}
        // 周期延时（正常流程）
        vTaskDelayUntil(&PreviousWakeTime, BUSI_TASK_INTERVAL_TIME);
    }
}


